﻿namespace WebPacker.Tests.Packs.Resources
{
	using System;
	using NUnit.Framework;
	using WebPacker.Packs.Resources;

	[TestFixture]
	public class ContentResourceTest
	{
		[Test]
		public void ConstructorTest()
		{
			var resource = new ContentResource("test");
			Assert.That(resource.GetLastModifiedTime(), Is.EqualTo(DateTime.Now).Within(1).Seconds);
			Assert.AreEqual("test", resource.Name);
			Assert.AreEqual(string.Empty, resource.GetContent());
		}

		[Test]
		public void Constructor2Test()
		{
			var resource = new ContentResource("test2", "hoho");
			Assert.That(resource.GetLastModifiedTime(), Is.EqualTo(DateTime.Now).Within(1).Seconds);
			Assert.AreEqual("test2", resource.Name);
			Assert.AreEqual("hoho", resource.GetContent());
		}

		[Test]
		public void Constructor3Test()
		{
			var lastModified = DateTime.Now.AddDays(-1);
			var resource = new ContentResource("test3", "hoho", lastModified);
			Assert.AreEqual("test3", resource.Name);
			Assert.AreEqual("hoho", resource.GetContent());
			Assert.AreEqual(lastModified, resource.GetLastModifiedTime());
		}

		[Test]
		public void SetContentTest()
		{
			var resource = new ContentResource("test");
			var content = "alert('test!')";
			resource.SetContent(content);
			Assert.That(resource.GetLastModifiedTime(), Is.EqualTo(DateTime.Now).Within(1).Seconds);
			Assert.AreEqual(content, resource.GetContent());
		}

		[Test]
		public void SetContent2Test()
		{
			var lastModified = DateTime.Now.AddDays(-1);
			var resource = new ContentResource("test");
			var content = "alert('test!')";
			resource.SetContent(content, lastModified);
			Assert.AreEqual(lastModified, resource.GetLastModifiedTime());
			Assert.AreEqual(content, resource.GetContent());
		}

		[Test]
		public void OnChangedTest()
		{
			bool signal = false;

			var resource = new ContentResource("test");
			var content = "alert('test!')";

			resource.OnChanged += (sender, e) =>
			{
				Assert.AreEqual(resource, sender);
				Assert.AreEqual(content, resource.GetContent());
				signal = true;
			};

			resource.SetContent(content);

			if (!signal)
			{
				Assert.Fail("Change event is not caused.");
			}
		}
	}
}
